"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.directoryTree = void 0;
const fs_1 = __importDefault(require("fs"));
const readline_1 = __importDefault(require("readline"));
const path_1 = require("path");
const index_1 = require("./index");
// 去除.gitignore文件中特殊符号
function trimSpecial(string) {
    if (string != "") {
        const pattern = /[`~!@#$%^&*()\+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/g;
        string = string.replace(pattern, "");
    }
    return string;
}
// 按行读取.gitignore文件
const readLine = () => __awaiter(void 0, void 0, void 0, function* () {
    return new Promise((resolve) => {
        const res = [];
        const rl = readline_1.default.createInterface({
            input: fs_1.default.createReadStream((0, path_1.join)((0, index_1.getProjectDir)(), ".gitignore")),
        });
        rl.on("line", (line) => {
            if (line.indexOf("#") === -1) {
                res.push(trimSpecial(line));
            }
        }).on("close", () => {
            resolve(res.filter(Boolean));
        });
    });
});
// 目录树生成
function directoryTree() {
    return __awaiter(this, void 0, void 0, function* () {
        let rlData = [];
        rlData = yield readLine();
        // 读取项目目录
        const readTree = (jsonPath = (0, index_1.getProjectDir)(), parentkey = "") => {
            const data = [];
            const findDirectory = (path) => {
                const files = fs_1.default.readdirSync(path);
                files.forEach((item) => {
                    const fPath = (0, path_1.join)(path, item);
                    const stat = fs_1.default.statSync(fPath);
                    if (stat.isDirectory() === true &&
                        !rlData.includes(item) &&
                        !item.includes(".")) {
                        data.push({
                            title: item,
                            key: parentkey + item,
                            children: readTree(fPath, parentkey + item + "/"),
                        });
                    }
                });
            };
            findDirectory(jsonPath);
            return data;
        };
        return readTree();
    });
}
exports.directoryTree = directoryTree;
